package service

import (
	"github.com/wzh200x/hyperNexus/hyperNexusProtocol/commonProtocol"
	"github.com/wzh200x/webServer/constants"
	Server "github.com/wzh200x/webServer/server"
	"google.golang.org/protobuf/types/known/emptypb"
)

// GameConfigResponse 游戏配置响应结构
type GameConfigResponse struct {
	Configs []GameConfig `json:"configs"`
}

// GameConfig 游戏配置结构
type GameConfig struct {
	ID         int32  `json:"id"`
	CreateTime string `json:"createTime"`
	UpdateTime string `json:"updateTime"`
	Name       string `json:"name"`
	Type       int32  `json:"type"`
	ParentId   int32  `json:"parentId"`
	Value      string `json:"value"`
	IsActive   int32  `json:"isActive"`
	Platform   int32  `json:"platform"`
}

// GetGameConfigs 获取游戏配置信息
func GetGameConfigs() (*GameConfigResponse, int, string) {
	// 获取DbAgentServerClient
	dbAgentClient := Server.ServerApp.DbAgentServerClient.GetClientProxy()

	// 调用dbAgentServer获取游戏配置信息
	gameConfigResp, queryErr := dbAgentClient.QueryGameConfig(emptypb.Empty{})
	if queryErr != nil {
		return nil, constants.ErrUnknown, "获取游戏配置信息失败: " + queryErr.Error()
	}

	// 转换为响应结构
	response := &GameConfigResponse{
		Configs: make([]GameConfig, 0),
	}

	// 将proto结构转换为响应结构
	if gameConfigResp.Configs != nil {
		for _, config := range gameConfigResp.Configs {
			if config != nil {
				gameConfig := GameConfig{
					ID:         config.ID,
					CreateTime: config.CreateTime,
					UpdateTime: config.UpdateTime,
					Name:       config.Name,
					Type:       config.Type,
					ParentId:   config.ParentId,
					Value:      config.Value,
					IsActive:   config.IsActive,
					Platform:   config.Platform,
				}
				response.Configs = append(response.Configs, gameConfig)
			}
		}
	}

	return response, 0, ""
}

// ConvertProtoGameConfig 将proto的GameConfig转换为服务层的GameConfig
func ConvertProtoGameConfig(protoConfig *commonProtocol.GameConfig) *GameConfig {
	if protoConfig == nil {
		return nil
	}

	return &GameConfig{
		ID:         protoConfig.ID,
		CreateTime: protoConfig.CreateTime,
		UpdateTime: protoConfig.UpdateTime,
		Name:       protoConfig.Name,
		Type:       protoConfig.Type,
		ParentId:   protoConfig.ParentId,
		Value:      protoConfig.Value,
		IsActive:   protoConfig.IsActive,
		Platform:   protoConfig.Platform,
	}
}
